<?php
/*************************************************************************

  $Id: IclearWrapperBase.php 279 2009-06-29 21:09:07Z dis $

  iclear payment system - because secure is simply secure
  http://www.iclear.de

  Copyright (c) 2001 - 2009 iclear

  Released under the GNU General Public License

************************************************************************

                    All rights reserved.

  This program is free software licensed under the GNU General Public License (GPL).

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA

*************************************************************************/

  class IclearWrapperBase extends IclearBase {
  	
  	var $icCore = false;
  	
  	var $id = '';
  	
  	var $icTableExist = false;
  	
  	var $currentProductID = 0;
  	
  	function IclearWrapperBase(&$icCore) {
  		$this->icVersion = '$Id: IclearWrapperBase.php 279 2009-06-29 21:09:07Z dis $';
  		parent::IclearBase($icCore);
  		$this->icTable = $this->iclearTable();
      $this->icTableExist = $this->dbFetchRecord('SHOW TABLES LIKE "' . $this->icTable . '"') ? true : false;
  	}
  	
  	function id() {
  		return $this->id;
  	}
  	
  	function tableExist() {
  		return $this->icTableExist;
  	}
  	
    function &loadBasket($basketID = '') {
      $rv = false;

      if($this->icTableExist && $basketID) {
      	if(!class_exists('IclearBasket')) {
      		require_once $this->icCore->getPath('iclear').'IclearBasket.php';
      	}
				if(!class_exists('IclearAddress')) {
				  require_once $this->icCore->getPath('iclear') . 'IclearAddress.php';
				}

        $qry = 'SELECT * FROM ' . $this->icTable . ' WHERE ' .
          'basketID = "'.$basketID.'"';#

        if($rec = $this->dbFetchRecord($qry)) {
        	if($rv = unserialize(base64_decode($rec['basket']))) {
// if there's an internal WSDL version mismatch, update basket item keys
           if($rv->icWsdlVersion != $this->icWsdlVersion) {
        			$rv->update();
        		}
        		$rv->iclearID($rec['iclearID']);

// recreate basket core reference
		        $this->icCore->setObject('IclearBasket', $rv);
		        $rv->icCore =& $this->icCore;
        	}
        }
      }

      return $rv;
    }
    
    function storeBasket($basket = false) {
    	$rc = false;
    	if(!$basket) {
    		$basket =& $this->icCore->getBasket();
    	}

    	if($this->icTableExist && $basket && $basket->basketItemCount()) {
    		$iclearID = $basket->iclearID();

// remove core from basket 2 prevent incomplete classes and overhead
// used unset - delete would kill the base core object!
    		unset($basket->icCore);
				$qry = 'REPLACE INTO ' . $this->icTable .
				  '(iclearID, basketID, basket) VALUES ('.
				  ($iclearID ? '"' . $iclearID . '"' : 'NULL'). ','.
          '"'.$basket->basketID().'",' .
				  '"'.base64_encode(serialize($basket)).'"'.
				')';
			  if($rc = $this->dbQuery($qry)) {
			  	$basket->iclearID($this->dbLastInsertID());
			  	
			  }
        $basket->icCore =& $this->icCore;
        
    	}
    	return $rc;
    }
    
    function deleteBasket($iclearID = 0) {
    	$rc = false;
    	if($iclearID) {
				$qry = 'DELETE FROM ' . $this->icTable .
				  ' WHERE iclearID = "'.$iclearID.'"';
			  $rc = $this->dbQuery($qry);
    	}
    	return $rc;
    }
    
    
    function installTable($clean = true) {
    	$rc = false;
    	if($clean) {
        $this->dropTable();
    	}
    	
    	$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->icTable . ' (' .
    	       'iclearID INT(11) NOT NULL AUTO_INCREMENT,' .
    	       'basketID VARCHAR(32) NOT NULL,' .
    	       'basket LONGTEXT NOT NULL,' .
    	       'ts TIMESTAMP NOT NULL, ' .
    	       'PRIMARY KEY (iclearID),'.
    	       'UNIQUE KEY basketID (basketID))';
    	       
      return $this->dbQuery($sql);
    }
    
    function dropTable() {
    	return $this->dbQuery("DROP TABLE IF EXISTS " . $this->icTable);
    }
    
    function iclearTable() {
    	return IC_TABLE_ORDERS;
    }
    
    function rewindOrder() {
    	$this->currentProductID = 0;
    }
    
    
    /**
     * Pseudo abstract methods follows
     */
    
    function finalizeBasket() {
    	return false;
    }
    
    /**
     *  abstract methods follows
     */
    
    function dbQuery($qry = '') {
    	die('Abstract method ' . __FUNCTION__ . 'called!');
    }
  	
    function dbQuote($val) {
      die('Abstract method ' . __FUNCTION__ . 'called!');
    }
    
    function dbFetchRecord($qry = '') {
    	die('Abstract method ' . __FUNCTION__ . 'called!');
    }
  	
    function dbLastInsertID() {
    	die('Abstract method ' . __FUNCTION__ . 'called!');
    }
  	
  }


?>